use crate::{
    controller, dto,
    http::server::{
        error::Error, handler::result::IntoJsonResponseResult, middleware::ValidatedJson,
    },
};
use axum::{extract::State, Json};
use std::sync::Arc;

pub struct EndpointPeerHandler {
    controller_set: Arc<controller::ControllerSet>,
}

impl EndpointPeerHandler {
    pub fn new(controller_set: Arc<controller::ControllerSet>) -> Self {
        Self { controller_set }
    }
    pub async fn add(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint_peer::AddRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::core::endpoint_peer::AddResponseData>>, Error>
    {
        state
            .controller_set
            .endpoint_peer
            .add(&request.0)
            .await
            .into_json_response_result()
    }
    pub async fn list(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint_peer::ListRequest>,
    ) -> Result<
        Json<wanmesh_common::dto::Response<dto::endpoint_peer::ListResponseData>>,
        Error,
    > {
        state
            .controller_set
            .endpoint_peer
            .list(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn get(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint_peer::GetRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::core::endpoint_peer::EndpointPeerItem>>, Error>
    {
        state
            .controller_set
            .endpoint_peer
            .get(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn delete(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::endpoint_peer::DeleteRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .endpoint_peer
            .delete(&request.0)
            .await
            .into_json_response_result()
    }
}
